package unsafe;

import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @projectName: JUC
 * @package: unsafe
 * @className: SetTest
 * @author: 冷环渊 doomwatcher
 * @description: TODO
 * @date: 2022/3/2 10:57
 * @version: 1.0
 */
public class SetTest {
    //java.util.ConcurrentModificationException 同理和集合没有什么区别
    public static void main(String[] args) {
        /* 如何解决hashset 线程不安全?
         * 1. 首先就是 Collections工具类 Collections.synchronizedSet(new HashSet());
         * 2. 就是和集合再同一个解决方案里的  Set set = new CopyOnWriteArraySet();
         * */
        Set set = new CopyOnWriteArraySet();
        for (int i = 1; i <= 600; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0, 5));
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
    }
}
